<template>
  <div>
    <UpLoadexcel :on-success="onSuccess" />
  </div>
</template>

<script>
import { formatExcelDate } from '@/utils/index'
import { importEmployee } from '@/api/employees'
export default {
  methods: {
    // 上传成功后
    async  onSuccess({ header, results }) {
      const arr = this.transExcel(results)
      // console.log(arr)
      const res = await importEmployee(arr)
      this.$message.success(res.message)
      this.$router.back()
    },
    // 格式化数据
    transExcel(results) {
      const userRelations = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber',
        '部门': 'departmentName',
        '聘用形式': 'formOfEmployment'
      }

      return results.map(item => {
        // 处理后的数据
        const obj = {}
        // 中文key名
        const chinaKey = Object.keys(item)
        chinaKey.forEach(key => {
          // 英文的key名
          const Ekeys = userRelations[key]
          if (Ekeys === 'timeOfEntry' || Ekeys === 'correctionTime') {
            obj[Ekeys] = new Date(formatExcelDate(item[key]))
          } else {
            obj[Ekeys] = item[key]
          }
        })
        return obj
      })
    }
  }
}
</script>

<style>

</style>
